home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / windownt / awksrc.zip / GAWK-D~1.14 / GAWK~5.INF (.txt) < prev    next >
GNU Info File  |  1993-10-03  |  51KB  |  966 lines

  1. This is Info file gawk.info, produced by Makeinfo-1.47 from the input
  2. file gawk.texi.
  3.    This file documents `awk', a program that you can use to select
  4. particular records in a file and perform operations upon them.
  5.    This is Edition 0.14 of `The GAWK Manual',
  6. for the 2.14 version of the GNU implementation
  7. of AWK.
  8.    Copyright (C) 1989, 1991, 1992 Free Software Foundation, Inc.
  9.    Permission is granted to make and distribute verbatim copies of this
  10. manual provided the copyright notice and this permission notice are
  11. preserved on all copies.
  12.    Permission is granted to copy and distribute modified versions of
  13. this manual under the conditions for verbatim copying, provided that
  14. the entire resulting derived work is distributed under the terms of a
  15. permission notice identical to this one.
  16.    Permission is granted to copy and distribute translations of this
  17. manual into another language, under the above conditions for modified
  18. versions, except that this permission notice may be stated in a
  19. translation approved by the Foundation.
  20. File: gawk.info,  Node: Expressions,  Next: Statements,  Prev: Actions,  Up: Top
  21. Expressions as Action Statements
  22. ********************************
  23.    Expressions are the basic building block of `awk' actions.  An
  24. expression evaluates to a value, which you can print, test, store in a
  25. variable or pass to a function.  But beyond that, an expression can
  26. assign a new value to a variable or a field, with an assignment
  27. operator.
  28.    An expression can serve as a statement on its own.  Most other kinds
  29. of statements contain one or more expressions which specify data to be
  30. operated on.  As in other languages, expressions in `awk' include
  31. variables, array references, constants, and function calls, as well as
  32. combinations of these with various operators.
  33. * Menu:
  34. * Constants::                   String, numeric, and regexp constants.
  35. * Variables::                   Variables give names to values for later use.
  36. * Arithmetic Ops::              Arithmetic operations (`+', `-', etc.)
  37. * Concatenation::               Concatenating strings.
  38. * Comparison Ops::              Comparison of numbers and strings
  39.                                 with `<', etc.
  40. * Boolean Ops::                 Combining comparison expressions
  41.                                 using boolean operators
  42.                                 `||' ("or"), `&&' ("and") and `!' ("not").
  43. * Assignment Ops::              Changing the value of a variable or a field.
  44. * Increment Ops::               Incrementing the numeric value of a variable.
  45. * Conversion::                  The conversion of strings to numbers
  46.                                 and vice versa.
  47. * Values::                      The whole truth about numbers and strings.
  48. * Conditional Exp::             Conditional expressions select
  49.                                 between two subexpressions under control
  50.                                 of a third subexpression.
  51. * Function Calls::              A function call is an expression.
  52. * Precedence::                  How various operators nest.
  53. File: gawk.info,  Node: Constants,  Next: Variables,  Prev: Expressions,  Up: Expressions
  54. Constant Expressions
  55. ====================
  56.    The simplest type of expression is the "constant", which always has
  57. the same value.  There are three types of constants: numeric constants,
  58. string constants, and regular expression constants.
  59.    A "numeric constant" stands for a number.  This number can be an
  60. integer, a decimal fraction, or a number in scientific (exponential)
  61. notation.  Note that all numeric values are represented within `awk' in
  62. double-precision floating point.  Here are some examples of numeric
  63. constants, which all have the same value:
  64.      105
  65.      1.05e+2
  66.      1050e-1
  67.    A string constant consists of a sequence of characters enclosed in
  68. double-quote marks.  For example:
  69.      "parrot"
  70. represents the string whose contents are `parrot'.  Strings in `gawk'
  71. can be of any length and they can contain all the possible 8-bit ASCII
  72. characters including ASCII NUL.  Other `awk' implementations may have
  73. difficulty with some character codes.
  74.    Some characters cannot be included literally in a string constant. 
  75. You represent them instead with "escape sequences", which are character
  76. sequences beginning with a backslash (`\').
  77.    One use of an escape sequence is to include a double-quote character
  78. in a string constant.  Since a plain double-quote would end the string,
  79. you must use `\"' to represent a single double-quote character as a
  80. part of the string. The backslash character itself is another character
  81. that cannot be included normally; you write `\\' to put one backslash
  82. in the string.  Thus, the string whose contents are the two characters
  83. `"\' must be written `"\"\\"'.
  84.    Another use of backslash is to represent unprintable characters such
  85. as newline.  While there is nothing to stop you from writing most of
  86. these characters directly in a string constant, they may look ugly.
  87.    Here is a table of all the escape sequences used in `awk':
  88.      Represents a literal backslash, `\'.
  89.      Represents the "alert" character, control-g, ASCII code 7.
  90.      Represents a backspace, control-h, ASCII code 8.
  91.      Represents a formfeed, control-l, ASCII code 12.
  92.      Represents a newline, control-j, ASCII code 10.
  93.      Represents a carriage return, control-m, ASCII code 13.
  94.      Represents a horizontal tab, control-i, ASCII code 9.
  95.      Represents a vertical tab, control-k, ASCII code 11.
  96. `\NNN'
  97.      Represents the octal value NNN, where NNN are one to three digits
  98.      between 0 and 7.  For example, the code for the ASCII ESC (escape)
  99.      character is `\033'.
  100. `\xHH...'
  101.      Represents the hexadecimal value HH, where HH are hexadecimal
  102.      digits (`0' through `9' and either `A' through `F' or `a' through
  103.      `f').  Like the same construct in ANSI C, the escape sequence
  104.      continues until the first non-hexadecimal digit is seen.  However,
  105.      using more than two hexadecimal digits produces undefined results.
  106.       (The `\x' escape sequence is not allowed in POSIX `awk'.)
  107.    A "constant regexp" is a regular expression description enclosed in
  108. slashes, such as `/^beginning and end$/'.  Most regexps used in `awk'
  109. programs are constant, but the `~' and `!~' operators can also match
  110. computed or "dynamic" regexps (*note How to Use Regular Expressions:
  111. Regexp Usage.).
  112.    Constant regexps may be used like simple expressions.  When a
  113. constant regexp is not on the right hand side of the `~' or `!~'
  114. operators, it has the same meaning as if it appeared in a pattern, i.e.
  115. `($0 ~ /foo/)' (*note Expressions as Patterns: Expression Patterns.).
  116. This means that the two code segments,
  117.      if ($0 ~ /barfly/ || $0 ~ /camelot/)
  118.          print "found"
  119.      if (/barfly/ || /camelot/)
  120.          print "found"
  121. are exactly equivalent.  One rather bizarre consequence of this rule is
  122. that the following boolean expression is legal, but does not do what
  123. the user intended:
  124.      if (/foo/ ~ $1) print "found foo"
  125.    This code is "obviously" testing `$1' for a match against the regexp
  126. `/foo/'.  But in fact, the expression `(/foo/ ~ $1)' actually means
  127. `(($0 ~ /foo/) ~ $1)'.  In other words, first match the input record
  128. against the regexp `/foo/'.  The result will be either a 0 or a 1,
  129. depending upon the success or failure of the match.  Then match that
  130. result against the first field in the record.
  131.    Since it is unlikely that you would ever really wish to make this
  132. kind of test, `gawk' will issue a warning when it sees this construct in
  133. a program.
  134.    Another consequence of this rule is that the assignment statement
  135.      matches = /foo/
  136. will assign either 0 or 1 to the variable `matches', depending upon the
  137. contents of the current input record.
  138.    Constant regular expressions are also used as the first argument for
  139. the `sub' and `gsub' functions (*note Built-in Functions for String
  140. Manipulation: String Functions.).
  141.    This feature of the language was never well documented until the
  142. POSIX specification.
  143.    You may be wondering, when is
  144.      $1 ~ /foo/ { ... }
  145. preferable to
  146.      $1 ~ "foo" { ... }
  147.    Since the right-hand sides of both `~' operators are constants, it
  148. is more efficient to use the `/foo/' form: `awk' can note that you have
  149. supplied a regexp and store it internally in a form that makes pattern
  150. matching more efficient.  In the second form, `awk' must first convert
  151. the string into this internal form, and then perform the pattern
  152. matching.  The first form is also better style; it shows clearly that
  153. you intend a regexp match.
  154. File: gawk.info,  Node: Variables,  Next: Arithmetic Ops,  Prev: Constants,  Up: Expressions
  155. Variables
  156. =========
  157.    Variables let you give names to values and refer to them later.  You
  158. have already seen variables in many of the examples.  The name of a
  159. variable must be a sequence of letters, digits and underscores, but it
  160. may not begin with a digit.  Case is significant in variable names; `a'
  161. and `A' are distinct variables.
  162.    A variable name is a valid expression by itself; it represents the
  163. variable's current value.  Variables are given new values with
  164. "assignment operators" and "increment operators". *Note Assignment
  165. Expressions: Assignment Ops.
  166.    A few variables have special built-in meanings, such as `FS', the
  167. field separator, and `NF', the number of fields in the current input
  168. record.  *Note Built-in Variables::, for a list of them.  These
  169. built-in variables can be used and assigned just like all other
  170. variables, but their values are also used or changed automatically by
  171. `awk'.  Each built-in variable's name is made entirely of upper case
  172. letters.
  173.    Variables in `awk' can be assigned either numeric or string values. 
  174. By default, variables are initialized to the null string, which is
  175. effectively zero if converted to a number.  There is no need to
  176. "initialize" each variable explicitly in `awk', the way you would in C
  177. or most other traditional languages.
  178. * Menu:
  179. * Assignment Options::          Setting variables on the command line
  180.                                 and a summary of command line syntax.
  181.                                 This is an advanced method of input.
  182. File: gawk.info,  Node: Assignment Options,  Prev: Variables,  Up: Variables
  183. Assigning Variables on the Command Line
  184. ---------------------------------------
  185.    You can set any `awk' variable by including a "variable assignment"
  186. among the arguments on the command line when you invoke `awk' (*note
  187. Invoking `awk': Command Line.).  Such an assignment has this form:
  188.      VARIABLE=TEXT
  189. With it, you can set a variable either at the beginning of the `awk'
  190. run or in between input files.
  191.    If you precede the assignment with the `-v' option, like this:
  192.      -v VARIABLE=TEXT
  193. then the variable is set at the very beginning, before even the `BEGIN'
  194. rules are run.  The `-v' option and its assignment must precede all the
  195. file name arguments, as well as the program text.
  196.    Otherwise, the variable assignment is performed at a time determined
  197. by its position among the input file arguments: after the processing of
  198. the preceding input file argument.  For example:
  199.      awk '{ print $n }' n=4 inventory-shipped n=2 BBS-list
  200. prints the value of field number `n' for all input records.  Before the
  201. first file is read, the command line sets the variable `n' equal to 4. 
  202. This causes the fourth field to be printed in lines from the file
  203. `inventory-shipped'.  After the first file has finished, but before the
  204. second file is started, `n' is set to 2, so that the second field is
  205. printed in lines from `BBS-list'.
  206.    Command line arguments are made available for explicit examination by
  207. the `awk' program in an array named `ARGV' (*note Built-in
  208. Variables::.).
  209.    `awk' processes the values of command line assignments for escape
  210. sequences (*note Constant Expressions: Constants.).
  211. File: gawk.info,  Node: Arithmetic Ops,  Next: Concatenation,  Prev: Variables,  Up: Expressions
  212. Arithmetic Operators
  213. ====================
  214.    The `awk' language uses the common arithmetic operators when
  215. evaluating expressions.  All of these arithmetic operators follow normal
  216. precedence rules, and work as you would expect them to.  This example
  217. divides field three by field four, adds field two, stores the result
  218. into field one, and prints the resulting altered input record:
  219.      awk '{ $1 = $2 + $3 / $4; print }' inventory-shipped
  220.    The arithmetic operators in `awk' are:
  221. `X + Y'
  222.      Addition.
  223. `X - Y'
  224.      Subtraction.
  225. `- X'
  226.      Negation.
  227. `+ X'
  228.      Unary plus.  No real effect on the expression.
  229. `X * Y'
  230.      Multiplication.
  231. `X / Y'
  232.      Division.  Since all numbers in `awk' are double-precision
  233.      floating point, the result is not rounded to an integer: `3 / 4'
  234.      has the value 0.75.
  235. `X % Y'
  236.      Remainder.  The quotient is rounded toward zero to an integer,
  237.      multiplied by Y and this result is subtracted from X. This
  238.      operation is sometimes known as "trunc-mod."  The following
  239.      relation always holds:
  240.           b * int(a / b) + (a % b) == a
  241.      One possibly undesirable effect of this definition of remainder is
  242.      that `X % Y' is negative if X is negative.  Thus,
  243.           -17 % 8 = -1
  244.      In other `awk' implementations, the signedness of the remainder
  245.      may be machine dependent.
  246. `X ^ Y'
  247. `X ** Y'
  248.      Exponentiation: X raised to the Y power.  `2 ^ 3' has the value 8.
  249.       The character sequence `**' is equivalent to `^'.  (The POSIX
  250.      standard only specifies the use of `^' for exponentiation.)
  251. File: gawk.info,  Node: Concatenation,  Next: Comparison Ops,  Prev: Arithmetic Ops,  Up: Expressions
  252. String Concatenation
  253. ====================
  254.    There is only one string operation: concatenation.  It does not have
  255. a specific operator to represent it.  Instead, concatenation is
  256. performed by writing expressions next to one another, with no operator.
  257.  For example:
  258.      awk '{ print "Field number one: " $1 }' BBS-list
  259. produces, for the first record in `BBS-list':
  260.      Field number one: aardvark
  261.    Without the space in the string constant after the `:', the line
  262. would run together.  For example:
  263.      awk '{ print "Field number one:" $1 }' BBS-list
  264. produces, for the first record in `BBS-list':
  265.      Field number one:aardvark
  266.    Since string concatenation does not have an explicit operator, it is
  267. often necessary to insure that it happens where you want it to by
  268. enclosing the items to be concatenated in parentheses.  For example, the
  269. following code fragment does not concatenate `file' and `name' as you
  270. might expect:
  271.      file = "file"
  272.      name = "name"
  273.      print "something meaningful" > file name
  274. It is necessary to use the following:
  275.      print "something meaningful" > (file name)
  276.    We recommend you use parentheses around concatenation in all but the
  277. most common contexts (such as in the right-hand operand of `=').
  278. File: gawk.info,  Node: Comparison Ops,  Next: Boolean Ops,  Prev: Concatenation,  Up: Expressions
  279. Comparison Expressions
  280. ======================
  281.    "Comparison expressions" compare strings or numbers for
  282. relationships such as equality.  They are written using "relational
  283. operators", which are a superset of those in C.  Here is a table of
  284. them:
  285. `X < Y'
  286.      True if X is less than Y.
  287. `X <= Y'
  288.      True if X is less than or equal to Y.
  289. `X > Y'
  290.      True if X is greater than Y.
  291. `X >= Y'
  292.      True if X is greater than or equal to Y.
  293. `X == Y'
  294.      True if X is equal to Y.
  295. `X != Y'
  296.      True if X is not equal to Y.
  297. `X ~ Y'
  298.      True if the string X matches the regexp denoted by Y.
  299. `X !~ Y'
  300.      True if the string X does not match the regexp denoted by Y.
  301. `SUBSCRIPT in ARRAY'
  302.      True if array ARRAY has an element with the subscript SUBSCRIPT.
  303.    Comparison expressions have the value 1 if true and 0 if false.
  304.    The rules `gawk' uses for performing comparisons are based on those
  305. in draft 11.2 of the POSIX standard.  The POSIX standard introduced the
  306. concept of a "numeric string", which is simply a string that looks like
  307. a number, for example, `" +2"'.
  308.    When performing a relational operation, `gawk' considers the type of
  309. an operand to be the type it received on its last *assignment*, rather
  310. than the type of its last *use* (*note Numeric and String Values:
  311. Values.). This type is *unknown* when the operand is from an "external"
  312. source: field variables, command line arguments, array elements
  313. resulting from a `split' operation, and the value of an `ENVIRON'
  314. element. In this case only, if the operand is a numeric string, then it
  315. is considered to be of both string type and numeric type.  If at least
  316. one operand of a comparison is of string type only, then a string
  317. comparison is performed.  Any numeric operand will be converted to a
  318. string using the value of `CONVFMT' (*note Conversion of Strings and
  319. Numbers: Conversion.). If one operand of a comparison is numeric, and
  320. the other operand is either numeric or both numeric and string, then
  321. `gawk' does a numeric comparison.  If both operands have both types,
  322. then the comparison is numeric.  Strings are compared by comparing the
  323. first character of each, then the second character of each, and so on. 
  324. Thus `"10"' is less than `"9"'.  If there are two strings where one is
  325. a prefix of the other, the shorter string is less than the longer one. 
  326. Thus `"abc"' is less than `"abcd"'.
  327.    Here are some sample expressions, how `gawk' compares them, and what
  328. the result of the comparison is.
  329. `1.5 <= 2.0'
  330.      numeric comparison (true)
  331. `"abc" >= "xyz"'
  332.      string comparison (false)
  333. `1.5 != " +2"'
  334.      string comparison (true)
  335. `"1e2" < "3"'
  336.      string comparison (true)
  337. `a = 2; b = "2"'
  338. `a == b'
  339.      string comparison (true)
  340.      echo 1e2 3 | awk '{ print ($1 < $2) ? "true" : "false" }'
  341. prints `false' since both `$1' and `$2' are numeric strings and thus
  342. have both string and numeric types, thus dictating a numeric comparison.
  343.    The purpose of the comparison rules and the use of numeric strings is
  344. to attempt to produce the behavior that is "least surprising," while
  345. still "doing the right thing."
  346.    String comparisons and regular expression comparisons are very
  347. different. For example,
  348.      $1 == "foo"
  349. has the value of 1, or is true, if the first field of the current input
  350. record is precisely `foo'.  By contrast,
  351.      $1 ~ /foo/
  352. has the value 1 if the first field contains `foo', such as `foobar'.
  353.    The right hand operand of the `~' and `!~' operators may be either a
  354. constant regexp (`/.../'), or it may be an ordinary expression, in
  355. which case the value of the expression as a string is a dynamic regexp
  356. (*note How to Use Regular Expressions: Regexp Usage.).
  357.    In very recent implementations of `awk', a constant regular
  358. expression in slashes by itself is also an expression.  The regexp
  359. `/REGEXP/' is an abbreviation for this comparison expression:
  360.      $0 ~ /REGEXP/
  361.    In some contexts it may be necessary to write parentheses around the
  362. regexp to avoid confusing the `gawk' parser.  For example, `(/x/ - /y/)
  363. > threshold' is not allowed, but `((/x/) - (/y/)) > threshold' parses
  364. properly.
  365.    One special place where `/foo/' is *not* an abbreviation for `$0 ~
  366. /foo/' is when it is the right-hand operand of `~' or `!~'! *Note
  367. Constant Expressions: Constants, where this is discussed in more detail.
  368. File: gawk.info,  Node: Boolean Ops,  Next: Assignment Ops,  Prev: Comparison Ops,  Up: Expressions
  369. Boolean Expressions
  370. ===================
  371.    A "boolean expression" is a combination of comparison expressions or
  372. matching expressions, using the boolean operators "or" (`||'), "and"
  373. (`&&'), and "not" (`!'), along with parentheses to control nesting. 
  374. The truth of the boolean expression is computed by combining the truth
  375. values of the component expressions.
  376.    Boolean expressions can be used wherever comparison and matching
  377. expressions can be used.  They can be used in `if', `while' `do' and
  378. `for' statements.  They have numeric values (1 if true, 0 if false),
  379. which come into play if the result of the boolean expression is stored
  380. in a variable, or used in arithmetic.
  381.    In addition, every boolean expression is also a valid boolean
  382. pattern, so you can use it as a pattern to control the execution of
  383. rules.
  384.    Here are descriptions of the three boolean operators, with an
  385. example of each.  It may be instructive to compare these examples with
  386. the analogous examples of boolean patterns (*note Boolean Operators and
  387. Patterns: Boolean Patterns.), which use the same boolean operators in
  388. patterns instead of expressions.
  389. `BOOLEAN1 && BOOLEAN2'
  390.      True if both BOOLEAN1 and BOOLEAN2 are true.  For example, the
  391.      following statement prints the current input record if it contains
  392.      both `2400' and `foo'.
  393.           if ($0 ~ /2400/ && $0 ~ /foo/) print
  394.      The subexpression BOOLEAN2 is evaluated only if BOOLEAN1 is true. 
  395.      This can make a difference when BOOLEAN2 contains expressions that
  396.      have side effects: in the case of `$0 ~ /foo/ && ($2 == bar++)',
  397.      the variable `bar' is not incremented if there is no `foo' in the
  398.      record.
  399. `BOOLEAN1 || BOOLEAN2'
  400.      True if at least one of BOOLEAN1 or BOOLEAN2 is true. For example,
  401.      the following command prints all records in the input file
  402.      `BBS-list' that contain *either* `2400' or `foo', or both.
  403.           awk '{ if ($0 ~ /2400/ || $0 ~ /foo/) print }' BBS-list
  404.      The subexpression BOOLEAN2 is evaluated only if BOOLEAN1 is false.
  405.       This can make a difference when BOOLEAN2 contains expressions
  406.      that have side effects.
  407. `!BOOLEAN'
  408.      True if BOOLEAN is false.  For example, the following program
  409.      prints all records in the input file `BBS-list' that do *not*
  410.      contain the string `foo'.
  411.           awk '{ if (! ($0 ~ /foo/)) print }' BBS-list
  412. File: gawk.info,  Node: Assignment Ops,  Next: Increment Ops,  Prev: Boolean Ops,  Up: Expressions
  413. Assignment Expressions
  414. ======================
  415.    An "assignment" is an expression that stores a new value into a
  416. variable.  For example, let's assign the value 1 to the variable `z':
  417.      z = 1
  418.    After this expression is executed, the variable `z' has the value 1.
  419. Whatever old value `z' had before the assignment is forgotten.
  420.    Assignments can store string values also.  For example, this would
  421. store the value `"this food is good"' in the variable `message':
  422.      thing = "food"
  423.      predicate = "good"
  424.      message = "this " thing " is " predicate
  425. (This also illustrates concatenation of strings.)
  426.    The `=' sign is called an "assignment operator".  It is the simplest
  427. assignment operator because the value of the right-hand operand is
  428. stored unchanged.
  429.    Most operators (addition, concatenation, and so on) have no effect
  430. except to compute a value.  If you ignore the value, you might as well
  431. not use the operator.  An assignment operator is different; it does
  432. produce a value, but even if you ignore the value, the assignment still
  433. makes itself felt through the alteration of the variable.  We call this
  434. a "side effect".
  435.    The left-hand operand of an assignment need not be a variable (*note
  436. Variables::.); it can also be a field (*note Changing the Contents of a
  437. Field: Changing Fields.) or an array element (*note Arrays in `awk':
  438. Arrays.). These are all called "lvalues", which means they can appear
  439. on the left-hand side of an assignment operator. The right-hand operand
  440. may be any expression; it produces the new value which the assignment
  441. stores in the specified variable, field or array element.
  442.    It is important to note that variables do *not* have permanent types.
  443. The type of a variable is simply the type of whatever value it happens
  444. to hold at the moment.  In the following program fragment, the variable
  445. `foo' has a numeric value at first, and a string value later on:
  446.      foo = 1
  447.      print foo
  448.      foo = "bar"
  449.      print foo
  450. When the second assignment gives `foo' a string value, the fact that it
  451. previously had a numeric value is forgotten.
  452.    An assignment is an expression, so it has a value: the same value
  453. that is assigned.  Thus, `z = 1' as an expression has the value 1. One
  454. consequence of this is that you can write multiple assignments together:
  455.      x = y = z = 0
  456. stores the value 0 in all three variables.  It does this because the
  457. value of `z = 0', which is 0, is stored into `y', and then the value of
  458. `y = z = 0', which is 0, is stored into `x'.
  459.    You can use an assignment anywhere an expression is called for.  For
  460. example, it is valid to write `x != (y = 1)' to set `y' to 1 and then
  461. test whether `x' equals 1.  But this style tends to make programs hard
  462. to read; except in a one-shot program, you should rewrite it to get rid
  463. of such nesting of assignments.  This is never very hard.
  464.    Aside from `=', there are several other assignment operators that do
  465. arithmetic with the old value of the variable.  For example, the
  466. operator `+=' computes a new value by adding the right-hand value to
  467. the old value of the variable.  Thus, the following assignment adds 5
  468. to the value of `foo':
  469.      foo += 5
  470. This is precisely equivalent to the following:
  471.      foo = foo + 5
  472. Use whichever one makes the meaning of your program clearer.
  473.    Here is a table of the arithmetic assignment operators.  In each
  474. case, the right-hand operand is an expression whose value is converted
  475. to a number.
  476. `LVALUE += INCREMENT'
  477.      Adds INCREMENT to the value of LVALUE to make the new value of
  478.      LVALUE.
  479. `LVALUE -= DECREMENT'
  480.      Subtracts DECREMENT from the value of LVALUE.
  481. `LVALUE *= COEFFICIENT'
  482.      Multiplies the value of LVALUE by COEFFICIENT.
  483. `LVALUE /= QUOTIENT'
  484.      Divides the value of LVALUE by QUOTIENT.
  485. `LVALUE %= MODULUS'
  486.      Sets LVALUE to its remainder by MODULUS.
  487. `LVALUE ^= POWER'
  488. `LVALUE **= POWER'
  489.      Raises LVALUE to the power POWER. (Only the `^=' operator is
  490.      specified by POSIX.)
  491. File: gawk.info,  Node: Increment Ops,  Next: Conversion,  Prev: Assignment Ops,  Up: Expressions
  492. Increment Operators
  493. ===================
  494.    "Increment operators" increase or decrease the value of a variable
  495. by 1.  You could do the same thing with an assignment operator, so the
  496. increment operators add no power to the `awk' language; but they are
  497. convenient abbreviations for something very common.
  498.    The operator to add 1 is written `++'.  It can be used to increment
  499. a variable either before or after taking its value.
  500.    To pre-increment a variable V, write `++V'.  This adds 1 to the
  501. value of V and that new value is also the value of this expression. 
  502. The assignment expression `V += 1' is completely equivalent.
  503.    Writing the `++' after the variable specifies post-increment.  This
  504. increments the variable value just the same; the difference is that the
  505. value of the increment expression itself is the variable's *old* value.
  506.  Thus, if `foo' has the value 4, then the expression `foo++' has the
  507. value 4, but it changes the value of `foo' to 5.
  508.    The post-increment `foo++' is nearly equivalent to writing `(foo +=
  509. 1) - 1'.  It is not perfectly equivalent because all numbers in `awk'
  510. are floating point: in floating point, `foo + 1 - 1' does not
  511. necessarily equal `foo'.  But the difference is minute as long as you
  512. stick to numbers that are fairly small (less than a trillion).
  513.    Any lvalue can be incremented.  Fields and array elements are
  514. incremented just like variables.  (Use `$(i++)' when you wish to do a
  515. field reference and a variable increment at the same time.  The
  516. parentheses are necessary because of the precedence of the field
  517. reference operator, `$'.)
  518.    The decrement operator `--' works just like `++' except that it
  519. subtracts 1 instead of adding.  Like `++', it can be used before the
  520. lvalue to pre-decrement or after it to post-decrement.
  521.    Here is a summary of increment and decrement expressions.
  522. `++LVALUE'
  523.      This expression increments LVALUE and the new value becomes the
  524.      value of this expression.
  525. `LVALUE++'
  526.      This expression causes the contents of LVALUE to be incremented.
  527.      The value of the expression is the *old* value of LVALUE.
  528. `--LVALUE'
  529.      Like `++LVALUE', but instead of adding, it subtracts.  It
  530.      decrements LVALUE and delivers the value that results.
  531. `LVALUE--'
  532.      Like `LVALUE++', but instead of adding, it subtracts.  It
  533.      decrements LVALUE.  The value of the expression is the *old* value
  534.      of LVALUE.
  535. File: gawk.info,  Node: Conversion,  Next: Values,  Prev: Increment Ops,  Up: Expressions
  536. Conversion of Strings and Numbers
  537. =================================
  538.    Strings are converted to numbers, and numbers to strings, if the
  539. context of the `awk' program demands it.  For example, if the value of
  540. either `foo' or `bar' in the expression `foo + bar' happens to be a
  541. string, it is converted to a number before the addition is performed. 
  542. If numeric values appear in string concatenation, they are converted to
  543. strings.  Consider this:
  544.      two = 2; three = 3
  545.      print (two three) + 4
  546. This eventually prints the (numeric) value 27.  The numeric values of
  547. the variables `two' and `three' are converted to strings and
  548. concatenated together, and the resulting string is converted back to the
  549. number 23, to which 4 is then added.
  550.    If, for some reason, you need to force a number to be converted to a
  551. string, concatenate the null string with that number.  To force a string
  552. to be converted to a number, add zero to that string.
  553.    A string is converted to a number by interpreting a numeric prefix
  554. of the string as numerals: `"2.5"' converts to 2.5, `"1e3"' converts to
  555. 1000, and `"25fix"' has a numeric value of 25. Strings that can't be
  556. interpreted as valid numbers are converted to zero.
  557.    The exact manner in which numbers are converted into strings is
  558. controlled by the `awk' built-in variable `CONVFMT' (*note Built-in
  559. Variables::.). Numbers are converted using a special version of the
  560. `sprintf' function (*note Built-in Functions: Built-in.) with `CONVFMT'
  561. as the format specifier.
  562.    `CONVFMT''s default value is `"%.6g"', which prints a value with at
  563. least six significant digits.  For some applications you will want to
  564. change it to specify more precision.  Double precision on most modern
  565. machines gives you 16 or 17 decimal digits of precision.
  566.    Strange results can happen if you set `CONVFMT' to a string that
  567. doesn't tell `sprintf' how to format floating point numbers in a useful
  568. way. For example, if you forget the `%' in the format, all numbers will
  569. be converted to the same constant string.
  570.    As a special case, if a number is an integer, then the result of
  571. converting it to a string is *always* an integer, no matter what the
  572. value of `CONVFMT' may be.  Given the following code fragment:
  573.      CONVFMT = "%2.2f"
  574.      a = 12
  575.      b = a ""
  576. `b' has the value `"12"', not `"12.00"'.
  577.    Prior to the POSIX standard, `awk' specified that the value of
  578. `OFMT' was used for converting numbers to strings.  `OFMT' specifies
  579. the output format to use when printing numbers with `print'. `CONVFMT'
  580. was introduced in order to separate the semantics of conversions from
  581. the semantics of printing.  Both `CONVFMT' and `OFMT' have the same
  582. default value: `"%.6g"'.  In the vast majority of cases, old `awk'
  583. programs will not change their behavior. However, this use of `OFMT' is
  584. something to keep in mind if you must port your program to other
  585. implementations of `awk'; we recommend that instead of changing your
  586. programs, you just port `gawk' itself!
  587. File: gawk.info,  Node: Values,  Next: Conditional Exp,  Prev: Conversion,  Up: Expressions
  588. Numeric and String Values
  589. =========================
  590.    Through most of this manual, we present `awk' values (such as
  591. constants, fields, or variables) as *either* numbers *or* strings. 
  592. This is a convenient way to think about them, since typically they are
  593. used in only one way, or the other.
  594.    In truth though, `awk' values can be *both* string and numeric, at
  595. the same time.  Internally, `awk' represents values with a string, a
  596. (floating point) number, and an indication that one, the other, or both
  597. representations of the value are valid.
  598.    Keeping track of both kinds of values is important for execution
  599. efficiency:  a variable can acquire a string value the first time it is
  600. used as a string, and then that string value can be used until the
  601. variable is assigned a new value.  Thus, if a variable with only a
  602. numeric value is used in several concatenations in a row, it only has
  603. to be given a string representation once.  The numeric value remains
  604. valid, so that no conversion back to a number is necessary if the
  605. variable is later used in an arithmetic expression.
  606.    Tracking both kinds of values is also important for precise numerical
  607. calculations.  Consider the following:
  608.      a = 123.321
  609.      CONVFMT = "%3.1f"
  610.      b = a " is a number"
  611.      c = a + 1.654
  612. The variable `a' receives a string value in the concatenation and
  613. assignment to `b'.  The string value of `a' is `"123.3"'. If the
  614. numeric value was lost when it was converted to a string, then the
  615. numeric use of `a' in the last statement would lose information. `c'
  616. would be assigned the value 124.954 instead of 124.975. Such errors
  617. accumulate rapidly, and very adversely affect numeric computations.
  618.    Once a numeric value acquires a corresponding string value, it stays
  619. valid until a new assignment is made.  If `CONVFMT' (*note Conversion
  620. of Strings and Numbers: Conversion.) changes in the meantime, the old
  621. string value will still be used.  For example:
  622.      BEGIN {
  623.          CONVFMT = "%2.2f"
  624.          a = 123.456
  625.          b = a ""                # force `a' to have string value too
  626.          printf "a = %s\n", a
  627.          CONVFMT = "%.6g"
  628.          printf "a = %s\n", a
  629.          a += 0                  # make `a' numeric only again
  630.          printf "a = %s\n", a    # use `a' as string
  631.      }
  632. This program prints `a = 123.46' twice, and then prints `a = 123.456'.
  633.    *Note Conversion of Strings and Numbers: Conversion, for the rules
  634. that specify how string values are made from numeric values.
  635. File: gawk.info,  Node: Conditional Exp,  Next: Function Calls,  Prev: Values,  Up: Expressions
  636. Conditional Expressions
  637. =======================
  638.    A "conditional expression" is a special kind of expression with
  639. three operands.  It allows you to use one expression's value to select
  640. one of two other expressions.
  641.    The conditional expression looks the same as in the C language:
  642.      SELECTOR ? IF-TRUE-EXP : IF-FALSE-EXP
  643. There are three subexpressions.  The first, SELECTOR, is always
  644. computed first.  If it is "true" (not zero and not null) then
  645. IF-TRUE-EXP is computed next and its value becomes the value of the
  646. whole expression.  Otherwise, IF-FALSE-EXP is computed next and its
  647. value becomes the value of the whole expression.
  648.    For example, this expression produces the absolute value of `x':
  649.      x > 0 ? x : -x
  650.    Each time the conditional expression is computed, exactly one of
  651. IF-TRUE-EXP and IF-FALSE-EXP is computed; the other is ignored. This is
  652. important when the expressions contain side effects.  For example, this
  653. conditional expression examines element `i' of either array `a' or
  654. array `b', and increments `i'.
  655.      x == y ? a[i++] : b[i++]
  656. This is guaranteed to increment `i' exactly once, because each time one
  657. or the other of the two increment expressions is executed, and the
  658. other is not.
  659. File: gawk.info,  Node: Function Calls,  Next: Precedence,  Prev: Conditional Exp,  Up: Expressions
  660. Function Calls
  661. ==============
  662.    A "function" is a name for a particular calculation.  Because it has
  663. a name, you can ask for it by name at any point in the program.  For
  664. example, the function `sqrt' computes the square root of a number.
  665.    A fixed set of functions are "built-in", which means they are
  666. available in every `awk' program.  The `sqrt' function is one of these.
  667.  *Note Built-in Functions: Built-in, for a list of built-in functions
  668. and their descriptions.  In addition, you can define your own functions
  669. in the program for use elsewhere in the same program. *Note
  670. User-defined Functions: User-defined, for how to do this.
  671.    The way to use a function is with a "function call" expression,
  672. which consists of the function name followed by a list of "arguments"
  673. in parentheses.  The arguments are expressions which give the raw
  674. materials for the calculation that the function will do. When there is
  675. more than one argument, they are separated by commas.  If there are no
  676. arguments, write just `()' after the function name. Here are some
  677. examples:
  678.      sqrt(x^2 + y^2)      # One argument
  679.      atan2(y, x)          # Two arguments
  680.      rand()               # No arguments
  681.    *Do not put any space between the function name and the
  682. open-parenthesis!*  A user-defined function name looks just like the
  683. name of a variable, and space would make the expression look like
  684. concatenation of a variable with an expression inside parentheses. 
  685. Space before the parenthesis is harmless with built-in functions, but
  686. it is best not to get into the habit of using space to avoid mistakes
  687. with user-defined functions.
  688.    Each function expects a particular number of arguments.  For
  689. example, the `sqrt' function must be called with a single argument, the
  690. number to take the square root of:
  691.      sqrt(ARGUMENT)
  692.    Some of the built-in functions allow you to omit the final argument.
  693. If you do so, they use a reasonable default. *Note Built-in Functions:
  694. Built-in, for full details.  If arguments are omitted in calls to
  695. user-defined functions, then those arguments are treated as local
  696. variables, initialized to the null string (*note User-defined
  697. Functions: User-defined.).
  698.    Like every other expression, the function call has a value, which is
  699. computed by the function based on the arguments you give it.  In this
  700. example, the value of `sqrt(ARGUMENT)' is the square root of the
  701. argument.  A function can also have side effects, such as assigning the
  702. values of certain variables or doing I/O.
  703.    Here is a command to read numbers, one number per line, and print the
  704. square root of each one:
  705.      awk '{ print "The square root of", $1, "is", sqrt($1) }'
  706. File: gawk.info,  Node: Precedence,  Prev: Function Calls,  Up: Expressions
  707. Operator Precedence (How Operators Nest)
  708. ========================================
  709.    "Operator precedence" determines how operators are grouped, when
  710. different operators appear close by in one expression.  For example,
  711. `*' has higher precedence than `+'; thus, `a + b * c' means to multiply
  712. `b' and `c', and then add `a' to the product (i.e., `a + (b * c)').
  713.    You can overrule the precedence of the operators by using
  714. parentheses. You can think of the precedence rules as saying where the
  715. parentheses are assumed if you do not write parentheses yourself.  In
  716. fact, it is wise to always use parentheses whenever you have an unusual
  717. combination of operators, because other people who read the program may
  718. not remember what the precedence is in this case.  You might forget,
  719. too; then you could make a mistake.  Explicit parentheses will help
  720. prevent any such mistake.
  721.    When operators of equal precedence are used together, the leftmost
  722. operator groups first, except for the assignment, conditional and
  723. exponentiation operators, which group in the opposite order. Thus, `a -
  724. b + c' groups as `(a - b) + c'; `a = b = c' groups as `a = (b = c)'.
  725.    The precedence of prefix unary operators does not matter as long as
  726. only unary operators are involved, because there is only one way to
  727. parse them--innermost first.  Thus, `$++i' means `$(++i)' and `++$x'
  728. means `++($x)'.  However, when another operator follows the operand,
  729. then the precedence of the unary operators can matter. Thus, `$x^2'
  730. means `($x)^2', but `-x^2' means `-(x^2)', because `-' has lower
  731. precedence than `^' while `$' has higher precedence.
  732.    Here is a table of the operators of `awk', in order of increasing
  733. precedence:
  734. assignment
  735.      `=', `+=', `-=', `*=', `/=', `%=', `^=', `**='.  These operators
  736.      group right-to-left. (The `**=' operator is not specified by
  737.      POSIX.)
  738. conditional
  739.      `?:'.  This operator groups right-to-left.
  740. logical "or".
  741.      `||'.
  742. logical "and".
  743.      `&&'.
  744. array membership
  745.      `in'.
  746. matching
  747.      `~', `!~'.
  748. relational, and redirection
  749.      The relational operators and the redirections have the same
  750.      precedence level.  Characters such as `>' serve both as
  751.      relationals and as redirections; the context distinguishes between
  752.      the two meanings.
  753.      The relational operators are `<', `<=', `==', `!=', `>=' and `>'.
  754.      The I/O redirection operators are `<', `>', `>>' and `|'.
  755.      Note that I/O redirection operators in `print' and `printf'
  756.      statements belong to the statement level, not to expressions.  The
  757.      redirection does not produce an expression which could be the
  758.      operand of another operator.  As a result, it does not make sense
  759.      to use a redirection operator near another operator of lower
  760.      precedence, without parentheses.  Such combinations, for example
  761.      `print foo > a ? b : c', result in syntax errors.
  762. concatenation
  763.      No special token is used to indicate concatenation. The operands
  764.      are simply written side by side.
  765. add, subtract
  766.      `+', `-'.
  767. multiply, divide, mod
  768.      `*', `/', `%'.
  769. unary plus, minus, "not"
  770.      `+', `-', `!'.
  771. exponentiation
  772.      `^', `**'.  These operators group right-to-left. (The `**'
  773.      operator is not specified by POSIX.)
  774. increment, decrement
  775.      `++', `--'.
  776. field
  777.      `$'.
  778. File: gawk.info,  Node: Statements,  Next: Arrays,  Prev: Expressions,  Up: Top
  779. Control Statements in Actions
  780. *****************************
  781.    "Control statements" such as `if', `while', and so on control the
  782. flow of execution in `awk' programs.  Most of the control statements in
  783. `awk' are patterned on similar statements in C.
  784.    All the control statements start with special keywords such as `if'
  785. and `while', to distinguish them from simple expressions.
  786.    Many control statements contain other statements; for example, the
  787. `if' statement contains another statement which may or may not be
  788. executed.  The contained statement is called the "body".  If you want
  789. to include more than one statement in the body, group them into a
  790. single compound statement with curly braces, separating them with
  791. newlines or semicolons.
  792. * Menu:
  793. * If Statement::                Conditionally execute
  794.                                 some `awk' statements.
  795. * While Statement::             Loop until some condition is satisfied.
  796. * Do Statement::                Do specified action while looping until some
  797.                                 condition is satisfied.
  798. * For Statement::               Another looping statement, that provides
  799.                                 initialization and increment clauses.
  800. * Break Statement::             Immediately exit the innermost enclosing loop.
  801. * Continue Statement::          Skip to the end of the innermost
  802.                                 enclosing loop.
  803. * Next Statement::              Stop processing the current input record.
  804. * Next File Statement::         Stop processing the current file.
  805. * Exit Statement::              Stop execution of `awk'.
  806. File: gawk.info,  Node: If Statement,  Next: While Statement,  Prev: Statements,  Up: Statements
  807. The `if' Statement
  808. ==================
  809.    The `if'-`else' statement is `awk''s decision-making statement.  It
  810. looks like this:
  811.      if (CONDITION) THEN-BODY [else ELSE-BODY]
  812. CONDITION is an expression that controls what the rest of the statement
  813. will do.  If CONDITION is true, THEN-BODY is executed; otherwise,
  814. ELSE-BODY is executed (assuming that the `else' clause is present). 
  815. The `else' part of the statement is optional.  The condition is
  816. considered false if its value is zero or the null string, and true
  817. otherwise.
  818.    Here is an example:
  819.      if (x % 2 == 0)
  820.          print "x is even"
  821.      else
  822.          print "x is odd"
  823.    In this example, if the expression `x % 2 == 0' is true (that is,
  824. the value of `x' is divisible by 2), then the first `print' statement
  825. is executed, otherwise the second `print' statement is performed.
  826.    If the `else' appears on the same line as THEN-BODY, and THEN-BODY
  827. is not a compound statement (i.e., not surrounded by curly braces),
  828. then a semicolon must separate THEN-BODY from `else'.  To illustrate
  829. this, let's rewrite the previous example:
  830.      awk '{ if (x % 2 == 0) print "x is even"; else
  831.              print "x is odd" }'
  832. If you forget the `;', `awk' won't be able to parse the statement, and
  833. you will get a syntax error.
  834.    We would not actually write this example this way, because a human
  835. reader might fail to see the `else' if it were not the first thing on
  836. its line.
  837. File: gawk.info,  Node: While Statement,  Next: Do Statement,  Prev: If Statement,  Up: Statements
  838. The `while' Statement
  839. =====================
  840.    In programming, a "loop" means a part of a program that is (or at
  841. least can be) executed two or more times in succession.
  842.    The `while' statement is the simplest looping statement in `awk'. 
  843. It repeatedly executes a statement as long as a condition is true.  It
  844. looks like this:
  845.      while (CONDITION)
  846.        BODY
  847. Here BODY is a statement that we call the "body" of the loop, and
  848. CONDITION is an expression that controls how long the loop keeps
  849. running.
  850.    The first thing the `while' statement does is test CONDITION. If
  851. CONDITION is true, it executes the statement BODY. (CONDITION is true
  852. when the value is not zero and not a null string.)  After BODY has been
  853. executed, CONDITION is tested again, and if it is still true, BODY is
  854. executed again.  This process repeats until CONDITION is no longer
  855. true.  If CONDITION is initially false, the body of the loop is never
  856. executed.
  857.    This example prints the first three fields of each record, one per
  858. line.
  859.      awk '{ i = 1
  860.             while (i <= 3) {
  861.                 print $i
  862.                 i++
  863.             }
  864.      }'
  865. Here the body of the loop is a compound statement enclosed in braces,
  866. containing two statements.
  867.    The loop works like this: first, the value of `i' is set to 1. Then,
  868. the `while' tests whether `i' is less than or equal to three.  This is
  869. the case when `i' equals one, so the `i'-th field is printed.  Then the
  870. `i++' increments the value of `i' and the loop repeats.  The loop
  871. terminates when `i' reaches 4.
  872.    As you can see, a newline is not required between the condition and
  873. the body; but using one makes the program clearer unless the body is a
  874. compound statement or is very simple.  The newline after the open-brace
  875. that begins the compound statement is not required either, but the
  876. program would be hard to read without it.
  877. File: gawk.info,  Node: Do Statement,  Next: For Statement,  Prev: While Statement,  Up: Statements
  878. The `do'-`while' Statement
  879. ==========================
  880.    The `do' loop is a variation of the `while' looping statement. The
  881. `do' loop executes the BODY once, then repeats BODY as long as
  882. CONDITION is true.  It looks like this:
  883.      do
  884.        BODY
  885.      while (CONDITION)
  886.    Even if CONDITION is false at the start, BODY is executed at least
  887. once (and only once, unless executing BODY makes CONDITION true). 
  888. Contrast this with the corresponding `while' statement:
  889.      while (CONDITION)
  890.        BODY
  891. This statement does not execute BODY even once if CONDITION is false to
  892. begin with.
  893.    Here is an example of a `do' statement:
  894.      awk '{ i = 1
  895.             do {
  896.                print $0
  897.                i++
  898.             } while (i <= 10)
  899.      }'
  900. prints each input record ten times.  It isn't a very realistic example,
  901. since in this case an ordinary `while' would do just as well.  But this
  902. reflects actual experience; there is only occasionally a real use for a
  903. `do' statement.
  904. File: gawk.info,  Node: For Statement,  Next: Break Statement,  Prev: Do Statement,  Up: Statements
  905. The `for' Statement
  906. ===================
  907.    The `for' statement makes it more convenient to count iterations of a
  908. loop.  The general form of the `for' statement looks like this:
  909.      for (INITIALIZATION; CONDITION; INCREMENT)
  910.        BODY
  911. This statement starts by executing INITIALIZATION.  Then, as long as
  912. CONDITION is true, it repeatedly executes BODY and then INCREMENT. 
  913. Typically INITIALIZATION sets a variable to either zero or one,
  914. INCREMENT adds 1 to it, and CONDITION compares it against the desired
  915. number of iterations.
  916.    Here is an example of a `for' statement:
  917.      awk '{ for (i = 1; i <= 3; i++)
  918.                print $i
  919.      }'
  920. This prints the first three fields of each input record, one field per
  921. line.
  922.    In the `for' statement, BODY stands for any statement, but
  923. INITIALIZATION, CONDITION and INCREMENT are just expressions.  You
  924. cannot set more than one variable in the INITIALIZATION part unless you
  925. use a multiple assignment statement such as `x = y = 0', which is
  926. possible only if all the initial values are equal.  (But you can
  927. initialize additional variables by writing their assignments as
  928. separate statements preceding the `for' loop.)
  929.    The same is true of the INCREMENT part; to increment additional
  930. variables, you must write separate statements at the end of the loop.
  931. The C compound expression, using C's comma operator, would be useful in
  932. this context, but it is not supported in `awk'.
  933.    Most often, INCREMENT is an increment expression, as in the example
  934. above.  But this is not required; it can be any expression whatever. 
  935. For example, this statement prints all the powers of 2 between 1 and
  936.      for (i = 1; i <= 100; i *= 2)
  937.        print i
  938.    Any of the three expressions in the parentheses following the `for'
  939. may be omitted if there is nothing to be done there.  Thus,
  940. `for (;x > 0;)' is equivalent to `while (x > 0)'.  If the CONDITION is
  941. omitted, it is treated as TRUE, effectively yielding an "infinite loop"
  942. (i.e., a loop that will never terminate).
  943.    In most cases, a `for' loop is an abbreviation for a `while' loop,
  944. as shown here:
  945.      INITIALIZATION
  946.      while (CONDITION) {
  947.        BODY
  948.        INCREMENT
  949.      }
  950. The only exception is when the `continue' statement (*note The
  951. `continue' Statement: Continue Statement.) is used inside the loop;
  952. changing a `for' statement to a `while' statement in this way can
  953. change the effect of the `continue' statement inside the loop.
  954.    There is an alternate version of the `for' loop, for iterating over
  955. all the indices of an array:
  956.      for (i in array)
  957.          DO SOMETHING WITH array[i]
  958. *Note Arrays in `awk': Arrays, for more information on this version of
  959. the `for' loop.
  960.    The `awk' language has a `for' statement in addition to a `while'
  961. statement because often a `for' loop is both less work to type and more
  962. natural to think of.  Counting the number of iterations is very common
  963. in loops.  It can be easier to think of this counting as part of
  964. looping rather than as something to do inside the loop.
  965.    The next section has more complicated examples of `for' loops.
  966.